<?php

namespace Models;

/**
 * @author Evgeny Baranov <evgeny@baranov.eu>
 */
abstract class Base {

    const OP_READ = 1;
    const OP_WRITE = 2;
    const OP_DELETE = 4;

    protected $created;
    protected $updated;

    /**
     *
     * @param array $data
     */
    public function __construct(array $data = array()) {
        $this->fromArray($data);
    }

    /**
     *
     * @param array $data
     * @return self
     */
    public function fromArray(array $data) {
        foreach ($data as $k => $v) {
            if (property_exists($this, $k)) {
                $this->$k = $v;
            }
        }

        return $this;
    }

    /**
     * @return array
     */
    public function toArray() {
        return array();
    }

    /**
     * @param integer $operation
     * @param User $user
     * @return boolean
     */
    public function cando($operation, User $user) {
        if ($user->isAdmin()) {
            return true;
        } else {
            return false;
        }
    }

    public function getCreated($format = 'F j, Y') {
        return date($format, strtotime($this->created));
    }

    public function getUpdated($format = 'F j, Y') {
        return date($format, strtotime($this->updated));
    }
}